//
//  TestSelectedQuestionView.m
//  Learn
//
//  Created by zyl on 2024/6/3.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "TestSelectedQuestionView.h"

@interface TestSelectedQuestionView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *tableFootorView;

@property(nonatomic ,strong)UIView *contentView;


@end

@implementation TestSelectedQuestionView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.contentView];
        [self.contentView addSubview:self.tableView];
        [self.contentView addSubview:self.tableFootorView];
        self.contentView.y = lScreenHeight;
        self.hidden = YES;
        
        
        [self setBackgroundColor:lClearColor];
    }
    return self;
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    HSItem *it = self.titleArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld大题",(long)indexPath.row + 1];
    cell.textLabel.font = MFontWithSize(16);
    cell.textLabel.textColor = it.isRead? rgba(142, 142, 142, 1): rgba(51, 51, 51, 1);
    cell.accessoryView = [[UIImageView alloc] initWithImage:IMAGENAMED(@"ic_mine_arrow_black")];
    return cell;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 52;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self dismissView];
    if (self.blobk) {
        self.blobk(indexPath.row);
    }
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *t = [touches anyObject];
    CGPoint p = [t locationInView:self];
    if (p.y < self.contentView.y) {
        [self dismissView];
    }
}
 - (void)finfishBtnAction
{
    [self dismissView];
    if (self.blobk) {
        self.blobk(10000);
    }
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, lScreenHeight - lTopBarHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:lWhiteColor];
        [_tableView setSeparatorColor:rgba(229, 229, 229, 1)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 0)];
    
    }
    
    return _tableView;
}
-(UIView *)contentView
{
    if (!_contentView)
    {
        _contentView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, 146)];
        [_contentView setBackgroundColor:lWhiteColor];
    }
    return _contentView;
}
-(UIView *)tableFootorView
{
    if (!_tableFootorView)
    {
        _tableFootorView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, 75)];
        _tableFootorView.backgroundColor = lClearColor;
        UIButton *finfishBtn = [UIButton new];
        finfishBtn.frame =CGRectMake(0, 0, lScreenWidth - 20, 45);
       
        finfishBtn.selected =NO;
        [finfishBtn setTitle:@"完成" forState:UIControlStateNormal];
        [finfishBtn setTitleColor:lWhiteColor forState:UIControlStateNormal];
        [finfishBtn.titleLabel setFont:MFontWithSize(18)];
        [finfishBtn addTarget:self action:@selector(finfishBtnAction) forControlEvents:UIControlEventTouchUpInside];
        finfishBtn.backgroundColor = lClearColor;
        UIView *btnView =[[UIView alloc] initWithFrame:CGRectMake((lScreenWidth - finfishBtn.width)/2.0, 20, finfishBtn.width, finfishBtn.height)];
        [btnView setAllCornersWithCornerRadius:finfishBtn.height/2.0];
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,btnView.width,btnView.height);
        gl.startPoint = CGPointMake(1, 0.5);
        gl.endPoint = CGPointMake(0, 0.5);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [btnView.layer addSublayer:gl];
        [btnView addSubview:finfishBtn];
        [_tableFootorView addSubview:btnView];
        [_tableFootorView setHeight:btnView.bottom];
    }
    
    return _tableFootorView;
    
}
- (void)showView
{
    self.hidden = NO;
    [self.tableView reloadData];
    [UIView animateWithDuration:0.2 animations:^{
        self.backgroundColor = [lBlackColor colorWithAlphaComponent:0.6];
        self.contentView.y = lScreenHeight - self.contentView.height;
    }];
}
-(void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    NSInteger count = self.titleArray.count > 6? 6:self.titleArray.count;
    self.tableView.height = 52*count;
    self.tableFootorView.y = self.tableView.height;
    self.contentView.height = self.tableFootorView.bottom + lBottomDangerousAreaHeight;
    [self.contentView setCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadius:15];
}
- (void)dismissView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.contentView.y = lScreenHeight;
        self.backgroundColor = [lBlackColor colorWithAlphaComponent:0];
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}
@end
